﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DataLayer;
using EntLib;
public partial class Widgets_Admin_initialize : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      if (!Roles.IsUserInRole("Administrator") && Profile.UserName.ToLower() != @"nusstf\cittka")
      {
         EntLibHelper.Log_Action(DateTime.Now, "ACCESS DENIED: " + Request.Url, Profile.UserName);
         return;
      }

      string[] eRoles = "Everyone,Administrators,NUS,NUS-Student,NUS-Staff,NUS-Alumni".Split(",".ToCharArray());
      foreach (string s in eRoles)
      {
         if (s.Length > 0)
         {
            Response.Write("<br />Role: " + s);

            if (Roles.RoleExists(s))
            {
               foreach (string u in Roles.FindUsersInRole(s, "%"))
               {
                  Response.Write("<br />Remove User: " + u);
                  Roles.RemoveUserFromRole(u, s);
               }

               //Response.Write("<br />Remove Role:" + s);
               //Roles.DeleteRole(s);
            }
            //Response.Write("<br />Create Role:" + s);
            //Roles.CreateRole(s);
         }
      }

      Response.Write(@"<br />Add Administrator: nusstf\cittka");
      Roles.AddUserToRole(@"nusstf\cittka", "Administrators");

      using (var db = DatabaseHelper.GetDashboardData())
      {
         foreach (var UserName in (from u in db.aspnet_Users orderby u.UserName select u.UserName))
         {
            LdapUser UserInfo = new LdapUser(UserName);
            switch (UserInfo.UserLdapType)
            {
               case UserType.NUSAlumni:
                  Response.Write("<br />Adding Alumni:" + UserName);
                  Roles.AddUserToRoles(UserName, "Everyone,NUS,NUS-Alumni".Split(",".ToCharArray()));
                  break;
               case UserType.NUSStaff:
                  Response.Write("<br />Adding Staff:" + UserName);
                  Roles.AddUserToRoles(UserName, "Everyone,NUS,NUS-Staff".Split(",".ToCharArray()));
                  break;
               case UserType.NUSStudent:
                  Response.Write("<br />Adding Student:" + UserName);
                  Roles.AddUserToRoles(UserName, "Everyone,NUS,NUS-Student".Split(",".ToCharArray()));
                  break;
            }
         }
      }
   }
}